package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.IEmployeeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements IEmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public ResultInfo login(String username, String password) {
        // 1.根据username调用mapper查询
        Employee employee = query().eq("username",username).one();
        if (employee == null) {
            return ResultInfo.error("员工不存在");
        }
        // 2.比对密码
        // 2-1 将前端密码进行加密
        String md5Pwd = SecureUtil.md5(password);
        // 2-2 取出数据密码，比对
        if (!StrUtil.equals(employee.getPassword(), md5Pwd)) { // 解决空指针问题
            return ResultInfo.error("密码不正确");
        }
        // 3.员工是否禁用
        if (employee.getStatus() == 0) {
            return ResultInfo.error("此员工被冻结，请联系管理员");
        }
        // 4.登录成功，返回结果
        return ResultInfo.success(employee);
    }

    //员工列表
    @Override
    public Page<Employee> findByPage(Integer pageNum, Integer pageSize, String name) {
        // 1.先查套餐基本信息
        // 1-1 构建条件对象
        LambdaQueryWrapper<Employee> employeeWrapper = new LambdaQueryWrapper<>();
        employeeWrapper.like(StrUtil.isNotEmpty(name), Employee::getName, name);
        // 1-2 构建分页对象
        Page<Employee> page = new Page<>(pageNum, pageSize);
        // 1-3 查询
        page = employeeMapper.selectPage(page, employeeWrapper);
        // 5.返回结果
        return page;
    }

    @Override
    public void saveEmployee(Employee employee) {
        // 1.补齐参数
        // 1-1 id  雪花算法ID生成器（课下作业）
        Long id = IdUtil.getSnowflake(1, 1).nextId();
        employee.setId(id);
        // 1-2 密码 md5加密
        String md5Pwd = SecureUtil.md5(Constant.INIT_PASSWORD);
        employee.setPassword(md5Pwd);
        // 1-3 员工状态
        employee.setStatus(Employee.STATUS_ENABLE);
//        // 创建、更新时间
//        employee.setCreateTime(new Date());
//        employee.setUpdateTime(new Date());
//        // 创建、更新人
//        employee.setCreateUser(1L);
//        employee.setUpdateUser(1L);

        // 2.调用mapper新增
        employeeMapper.insert(employee);
    }

    @Override
    public Employee findById(Long id) {
        // 直接调用mapper
        return employeeMapper.selectById(id);
    }

    @Override
    public void updateEmployee(Employee employee) {
//        // 1.补齐参数
//        employee.setUpdateTime(new Date());
//        employee.setUpdateUser(1L); // 暂时写死1L

        // 2.调用mapper修改
        employeeMapper.updateById(employee);
    }

}
